/*
 * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: CC0-1.0
 */

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_timer.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "esp_err.h"
#include "esp_log.h"
#include "lvgl.h"
// #include "lv_demo_benchmark.h"
#include "ui/ui.h"
#include "model/network/net_wifi.h"
#include "model/network/http_get.h"
#include "model/aht20/aht20.h"
#include <time.h>
#include <sys/time.h>

#if CONFIG_EXAMPLE_LCD_CONTROLLER_ILI9341
#include "esp_lcd_ili9341.h"
#elif CONFIG_EXAMPLE_LCD_CONTROLLER_GC9A01
#include "esp_lcd_gc9a01.h"
#elif CONFIG_EXAMPLE_LCD_CONTROLLER_ST7789
#include "esp_lcd_st7789.h"
#endif

#if CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_STMPE610
#include "esp_lcd_touch_stmpe610.h"
#endif

static const char *TAG = "example";

// Using SPI2 in the example
#define LCD_HOST  SPI2_HOST

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////// Please update the following configuration according to your LCD spec //////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define EXAMPLE_LCD_PIXEL_CLOCK_HZ     (20 * 1000 * 1000)
#define EXAMPLE_LCD_BK_LIGHT_ON_LEVEL  1
#define EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL !EXAMPLE_LCD_BK_LIGHT_ON_LEVEL
#define EXAMPLE_PIN_NUM_SCLK           2
#define EXAMPLE_PIN_NUM_MOSI           3
#define EXAMPLE_PIN_NUM_MISO           -1
#define EXAMPLE_PIN_NUM_LCD_DC         6
#define EXAMPLE_PIN_NUM_LCD_RST        10
#define EXAMPLE_PIN_NUM_LCD_CS         -1
#define EXAMPLE_PIN_NUM_BK_LIGHT       7
#define EXAMPLE_PIN_NUM_TOUCH_CS       -1

#define I2C_MASTER_SDA_IO 4
#define I2C_MASTER_SCL_IO 5
#define I2C_MASTER_FREQ_HZ 100 * 1000

// The pixel number in horizontal and vertical
#if CONFIG_EXAMPLE_LCD_CONTROLLER_ILI9341
#define EXAMPLE_LCD_H_RES              240
#define EXAMPLE_LCD_V_RES              320
#elif CONFIG_EXAMPLE_LCD_CONTROLLER_GC9A01
#define EXAMPLE_LCD_H_RES              240
#define EXAMPLE_LCD_V_RES              240
#elif CONFIG_EXAMPLE_LCD_CONTROLLER_ST7789
#define EXAMPLE_LCD_H_RES              240
#define EXAMPLE_LCD_V_RES              240
#endif
// Bit number used to represent command and parameter
#define EXAMPLE_LCD_CMD_BITS           8
#define EXAMPLE_LCD_PARAM_BITS         8

#define EXAMPLE_LVGL_TICK_PERIOD_MS    2


#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
esp_lcd_touch_handle_t tp = NULL;
#endif

extern void example_lvgl_demo_ui(lv_disp_t *disp);
extern void example_lvgl_widget_ui(lv_disp_t *disp);

static bool example_notify_lvgl_flush_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx)
{
    lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx;
    lv_disp_flush_ready(disp_driver);
    return false;
}

static void example_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
{
    esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data;
    int offsetx1 = area->x1;
    int offsetx2 = area->x2;
    int offsety1 = area->y1;
    int offsety2 = area->y2;
    // copy a buffer's content to a specific area of the display
    esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
}

/* Rotate display and touch, when rotated screen in LVGL. Called when driver parameters are updated. */
static void example_lvgl_port_update_callback(lv_disp_drv_t *drv)
{
    esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data;

    switch (drv->rotated) {
    case LV_DISP_ROT_NONE:
        // Rotate LCD display
        esp_lcd_panel_swap_xy(panel_handle, false);
        esp_lcd_panel_mirror(panel_handle, true, false);
#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
        // Rotate LCD touch
        esp_lcd_touch_set_mirror_y(tp, false);
        esp_lcd_touch_set_mirror_x(tp, false);
#endif
        break;
    case LV_DISP_ROT_90:
        // Rotate LCD display
        esp_lcd_panel_swap_xy(panel_handle, true);
        esp_lcd_panel_mirror(panel_handle, true, true);
#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
        // Rotate LCD touch
        esp_lcd_touch_set_mirror_y(tp, false);
        esp_lcd_touch_set_mirror_x(tp, false);
#endif
        break;
    case LV_DISP_ROT_180:
        // Rotate LCD display
        esp_lcd_panel_swap_xy(panel_handle, false);
        esp_lcd_panel_mirror(panel_handle, false, true);
#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
        // Rotate LCD touch
        esp_lcd_touch_set_mirror_y(tp, false);
        esp_lcd_touch_set_mirror_x(tp, false);
#endif
        break;
    case LV_DISP_ROT_270:
        // Rotate LCD display
        esp_lcd_panel_swap_xy(panel_handle, true);
        esp_lcd_panel_mirror(panel_handle, false, false);
#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
        // Rotate LCD touch
        esp_lcd_touch_set_mirror_y(tp, false);
        esp_lcd_touch_set_mirror_x(tp, false);
#endif
        break;
    }
}

#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
static void example_lvgl_touch_cb(lv_indev_drv_t * drv, lv_indev_data_t * data)
{
    uint16_t touchpad_x[1] = {0};
    uint16_t touchpad_y[1] = {0};
    uint8_t touchpad_cnt = 0;

    /* Read touch controller data */
    esp_lcd_touch_read_data(drv->user_data);

    /* Get coordinates */
    bool touchpad_pressed = esp_lcd_touch_get_coordinates(drv->user_data, touchpad_x, touchpad_y, NULL, &touchpad_cnt, 1);

    if (touchpad_pressed && touchpad_cnt > 0) {
        data->point.x = touchpad_x[0];
        data->point.y = touchpad_y[0];
        data->state = LV_INDEV_STATE_PRESSED;
    } else {
        data->state = LV_INDEV_STATE_RELEASED;
    }
}
#endif

static void example_increase_lvgl_tick(void *arg)
{
    /* Tell LVGL how many milliseconds has elapsed */
    lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
}

char img_show[16] = {0};
char temp_show[16] = {0};
char weather_show[1024] = {0};
char time_show[16];
char env_inhome_str[16];

static int aht20_loop_count = 10;

AHT20_data_t AHT20_data =
{
    .RH = 0,
    .TEMP = 0
};

static void tm_env_inhome_cb(lv_timer_t *timer)
{
    time_t now;
    struct tm timeinfo;
    memset(time_show, 0, sizeof(time_show));
    now = time(NULL);
    localtime_r(&now, &timeinfo);
    strftime(time_show, sizeof(time_show), "%H:%M:%S", &timeinfo);
    lv_label_set_text(ui_LabelTime, time_show);

    aht20_loop_count ++;
    if (aht20_loop_count > 10)
    {
        aht20_loop_count = 0;
         esp_err_t ret = AHT20_measure(&AHT20_data);
        if(ret == 0)
        {
            memset(env_inhome_str, 0, sizeof(env_inhome_str));
            sprintf(env_inhome_str, "温 %.1f°C", AHT20_data.TEMP);
            lv_label_set_text(ui_LabelInTemp, env_inhome_str);
            memset(env_inhome_str, 0, sizeof(env_inhome_str));
            sprintf(env_inhome_str, "湿 %.1f%%", AHT20_data.RH);
            lv_label_set_text(ui_LabelInRH, env_inhome_str);
        }
    }
}

void decode_weather(const char *weather_data, int len)
{
    const char *date_start = strstr(weather_data, "\"date\":\"");
    const char *location_start = strstr(weather_data, "\"cityname\":\"");
    const char *weather_start = strstr(weather_data, "\"weather\":\"");
    const char *wind_direction_start = strstr(weather_data, "\"WD\":\"");
    const char *wind_speed_start = strstr(weather_data, "\"WS\":\"");
    const char *temperature_start = strstr(weather_data, "\"temp\":\"");
    const char *humidity_start = strstr(weather_data, "\"SD\":\"");
    const char *pm25_start = strstr(weather_data, "\"aqi_pm25\":\"");
    const char *update_start = strstr(weather_data, "\"time\":\"");
    const char *tommer_start = strstr(weather_data, "\"rain24h\":\"");

    memset(img_show, 0, sizeof(img_show));
    memset(temp_show, 0, sizeof(temp_show));
    memset(weather_show, 0, sizeof(weather_show));

    ESP_LOGI(TAG, "weather_data %s\n", weather_data);

    if (date_start && location_start && weather_start && wind_direction_start && wind_speed_start && temperature_start && humidity_start && pm25_start && update_start && tommer_start)
    {
        date_start += 8;
        location_start += 12;
        weather_start += 11;
        wind_direction_start += 6;
        wind_speed_start += 6;
        temperature_start += 8;
        humidity_start += 6;
        pm25_start += 12;
        update_start += 8;
        tommer_start += 11;

        const char *date_end = strstr(date_start, "\"}");
        const char *location_end = strstr(location_start, "\",\"");
        const char *weather_end = strstr(weather_start, "\",\"");
        const char *wind_direction_end = strstr(wind_direction_start, "\",\"");
        const char *wind_speed_end = strstr(wind_speed_start, "\",\"");
        const char *temperature_end = strstr(temperature_start, "\",\"");
        const char *humidity_end = strstr(humidity_start, "\",\"");
        const char *pm25_end = strstr(pm25_start, "\",\"");
        const char *update_end = strstr(update_start, "\",\"");
        const char *tommer_end = strstr(tommer_start, "\",\"");

        if (date_end && location_end && weather_end && wind_direction_end && wind_speed_end && temperature_end && humidity_end && pm25_end && update_end && tommer_end)
        {
            size_t date_len = date_end - date_start;
            size_t location_len = location_end - location_start;
            size_t weather_len = weather_end - weather_start;
            size_t wind_direction_len = wind_direction_end - wind_direction_start;
            size_t wind_speed_len = wind_speed_end - wind_speed_start;
            size_t temperature_len = temperature_end - temperature_start;
            size_t humidity_len = humidity_end - humidity_start;
            size_t pm25_len = pm25_end - pm25_start;
            size_t update_len = update_end - update_start;
            size_t tommer_len = tommer_end - tommer_start;

            strncat(weather_show, date_start, date_len);
            strcat(weather_show, ",");
            strncat(weather_show, location_start, location_len);
            strcat(weather_show, ",");
            strncat(weather_show, weather_start, weather_len);
            strcat(weather_show, ",");
            strncat(weather_show, wind_direction_start, wind_direction_len);
            // strcat(weather_show,",");
            strncat(weather_show, wind_speed_start, wind_speed_len);
            strcat(weather_show, ",温度");
            strncat(weather_show, temperature_start, temperature_len);
            strcat(weather_show, "°C,湿度");
            strncat(weather_show, humidity_start, humidity_len);
            strcat(weather_show, ",PM2.5 ");
            strncat(weather_show, pm25_start, pm25_len);
            {
                strcat(img_show, "pm:");
                strncat(img_show, pm25_start, pm25_len); 
                lv_label_set_text(ui_LabelPM2dot5,img_show);
                memset(img_show, 0, sizeof(img_show));
            }
            strcat(weather_show, ",未来24小时:");
            // memcpy(img_show, tommer_start, tommer_len);
            // ESP_LOGI(TAG, "tommer_start %s\n", tommer_start);
            // memset(img_show, 0, sizeof(img_show));
            if (0 == memcmp(tommer_start, "0", 1))
            {
                strcat(weather_show, "无雨");
                lv_label_set_text(ui_LabelTomor,"24h无雨");
            }
            else
            {
                strcat(weather_show, "有雨");
                lv_label_set_text(ui_LabelTomor,"24h有雨");
            }
            strcat(weather_show, ",更新时间");
            strncat(weather_show, update_start, update_len);

            strncat(img_show, weather_start, weather_len);
            ESP_LOGI(TAG, "img_show %s\n", img_show);
            lv_label_set_text(ui_LabelWeatherIcom, img_show);

            strncat(temp_show, temperature_start, temperature_len);
            strcat(temp_show, "°C");
            ESP_LOGI(TAG, "temp_show %s\n", temp_show);
            lv_label_set_text(ui_LabelWeather, temp_show);

            ESP_LOGI(TAG, "weather_show %s\n", weather_show);
            lv_label_set_text(ui_LabelWetherDetail, weather_show);
        }
        else
        {
            ESP_LOGE(TAG, "Failed to parse weather data stop\n");
        }
    }
    else
    {
        ESP_LOGE(TAG, "Failed to parse weather data start\n");
    }
}

static void weather_get_task(void *pvParameters)
{
    static const char *REQUEST = "GET /sk_2d/101031100.html HTTP/1.1\r\n"
    "Host:d1.weather.com.cn\r\n"
    "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0\r\n"
    "Accept:*/*\r\n"
    "Accept-Language:zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2\r\n"
    "Accept-Encoding:text/plant\r\n"
    "Sec-GPC:1\r\n"
    "Connection:keep-alive\r\n"
    "Referer:https://mjs.weather.com.cn/\r\n"
    "Sec-Fetch-Dest:script\r\n"
    "Sec-Fetch-Mode:no-cors\r\n"
    "Sec-Fetch-Site:same-site\r\n"
    "Pragma:no-cache\r\n"
    "Cache-Control:no-cache\r\n"
    "\r\n";

    decode_pointer weather_func = decode_weather;

    http_get_task("d1.weather.com.cn","80",REQUEST,"var dataSK=",weather_func);
}

i2c_config_t conf =
{
    .mode = I2C_MODE_MASTER,
    .sda_io_num = I2C_MASTER_SDA_IO,
    .sda_pullup_en = GPIO_PULLUP_DISABLE,
    .scl_io_num = I2C_MASTER_SCL_IO,
    .scl_pullup_en = GPIO_PULLUP_DISABLE,
    .master.clk_speed = I2C_MASTER_FREQ_HZ,
};

void aht20_setup()
{
    i2c_param_config(I2C_MASTER_PORT, &conf);
    i2c_driver_install(I2C_MASTER_PORT, I2C_MODE_MASTER, 0, 0, 0);
}

void app_main(void)
{
    static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
    static lv_disp_drv_t disp_drv;      // contains callback functions

    ESP_LOGI(TAG, "Turn off LCD backlight");
    gpio_config_t bk_gpio_config = {
        .mode = GPIO_MODE_OUTPUT,
        .pin_bit_mask = 1ULL << EXAMPLE_PIN_NUM_BK_LIGHT
    };
    ESP_ERROR_CHECK(gpio_config(&bk_gpio_config));

    ESP_LOGI(TAG, "Initialize SPI bus");
    spi_bus_config_t buscfg = {
        .sclk_io_num = EXAMPLE_PIN_NUM_SCLK,
        .mosi_io_num = EXAMPLE_PIN_NUM_MOSI,
        .miso_io_num = EXAMPLE_PIN_NUM_MISO,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
        .max_transfer_sz = EXAMPLE_LCD_H_RES * 80 * sizeof(uint16_t),
    };
    ESP_ERROR_CHECK(spi_bus_initialize(LCD_HOST, &buscfg, SPI_DMA_CH_AUTO));

    ESP_LOGI(TAG, "Install panel IO");
    esp_lcd_panel_io_handle_t io_handle = NULL;
    esp_lcd_panel_io_spi_config_t io_config = {
        .dc_gpio_num = EXAMPLE_PIN_NUM_LCD_DC,
        .cs_gpio_num = EXAMPLE_PIN_NUM_LCD_CS,
        .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
        .lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS,
        .lcd_param_bits = EXAMPLE_LCD_PARAM_BITS,
        .spi_mode = 3,
        .trans_queue_depth = 7,
        .on_color_trans_done = example_notify_lvgl_flush_ready,
        .user_ctx = &disp_drv,
    };
    // Attach the LCD to the SPI bus
    ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST, &io_config, &io_handle));

    ESP_LOGI(TAG, "Install GC9A01 panel driver");
    esp_lcd_panel_handle_t panel_handle = NULL;
    esp_lcd_panel_dev_config_t panel_config = {
        .reset_gpio_num = EXAMPLE_PIN_NUM_LCD_RST,
#if CONFIG_EXAMPLE_LCD_CONTROLLER_ILI9341
        .rgb_endian = LCD_RGB_ENDIAN_RGB,
#elif CONFIG_EXAMPLE_LCD_CONTROLLER_GC9A01
        .rgb_endian = LCD_RGB_ENDIAN_BGR,
#elif CONFIG_EXAMPLE_LCD_CONTROLLER_ST7789
        .rgb_endian = LCD_RGB_ENDIAN_RGB,
#endif
        .bits_per_pixel = 16,
    };
#if CONFIG_EXAMPLE_LCD_CONTROLLER_ILI9341
    ESP_ERROR_CHECK(esp_lcd_new_panel_ili9341(io_handle, &panel_config, &panel_handle));
#elif CONFIG_EXAMPLE_LCD_CONTROLLER_GC9A01
    ESP_ERROR_CHECK(esp_lcd_new_panel_gc9a01(io_handle, &panel_config, &panel_handle));
#elif CONFIG_EXAMPLE_LCD_CONTROLLER_ST7789
    ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
#endif

    ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
    ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
#if CONFIG_EXAMPLE_LCD_CONTROLLER_GC9A01
    ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, true));
#endif
#if CONFIG_EXAMPLE_LCD_CONTROLLER_ST7789
    ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, true));
#endif
    ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, false, false));

    // user can flush pre-defined pattern to the screen before we turn on the screen or backlight
    ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));

#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
    esp_lcd_panel_io_handle_t tp_io_handle = NULL;
    esp_lcd_panel_io_spi_config_t tp_io_config = ESP_LCD_TOUCH_IO_SPI_STMPE610_CONFIG(EXAMPLE_PIN_NUM_TOUCH_CS);
    // Attach the TOUCH to the SPI bus
    ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST, &tp_io_config, &tp_io_handle));

    esp_lcd_touch_config_t tp_cfg = {
        .x_max = EXAMPLE_LCD_H_RES,
        .y_max = EXAMPLE_LCD_V_RES,
        .rst_gpio_num = -1,
        .int_gpio_num = -1,
        .flags = {
            .swap_xy = 0,
            .mirror_x = 0,
            .mirror_y = 0,
        },
    };

#if CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_STMPE610
    ESP_LOGI(TAG, "Initialize touch controller STMPE610");
    ESP_ERROR_CHECK(esp_lcd_touch_new_spi_stmpe610(tp_io_handle, &tp_cfg, &tp));
#endif // CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_STMPE610
#endif // CONFIG_EXAMPLE_LCD_TOUCH_ENABLED

    ESP_LOGI(TAG, "Turn on LCD backlight");
    gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_ON_LEVEL);

    ESP_LOGI(TAG, "Initialize LVGL library");
    lv_init();
    // alloc draw buffers used by LVGL
    // it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
    lv_color_t *buf1 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 20 * sizeof(lv_color_t), MALLOC_CAP_DMA);
    assert(buf1);
    lv_color_t *buf2 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 20 * sizeof(lv_color_t), MALLOC_CAP_DMA);
    assert(buf2);
    // initialize LVGL draw buffers
    lv_disp_draw_buf_init(&disp_buf, buf1, buf2, EXAMPLE_LCD_H_RES * 20);

    ESP_LOGI(TAG, "Register display driver to LVGL");
    lv_disp_drv_init(&disp_drv);
    disp_drv.hor_res = EXAMPLE_LCD_H_RES;
    disp_drv.ver_res = EXAMPLE_LCD_V_RES;
    disp_drv.flush_cb = example_lvgl_flush_cb;
    disp_drv.drv_update_cb = example_lvgl_port_update_callback;
    disp_drv.draw_buf = &disp_buf;
    disp_drv.user_data = panel_handle;
    lv_disp_t *disp = lv_disp_drv_register(&disp_drv);

    ESP_LOGI(TAG, "Install LVGL tick timer");
    // Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
    const esp_timer_create_args_t lvgl_tick_timer_args = {
        .callback = &example_increase_lvgl_tick,
        .name = "lvgl_tick"
    };
    esp_timer_handle_t lvgl_tick_timer = NULL;
    ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
    ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000));

#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
    static lv_indev_drv_t indev_drv;    // Input device driver (Touch)
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.disp = disp;
    indev_drv.read_cb = example_lvgl_touch_cb;
    indev_drv.user_data = tp;

    lv_indev_drv_register(&indev_drv);
#endif

    

    ESP_LOGI(TAG, "Display LVGL Meter Widget");
    // example_lvgl_demo_ui(disp);
    // example_lvgl_widget_ui(disp);
    ui_init();
    aht20_setup();
    if(net_wifi_connect() == ESP_OK)
    {
        obtain_time();
        xTaskCreate(&weather_get_task, "weather_get_task", 4096, NULL, 5, NULL);
        // weather_get_task(NULL);
    }

    lv_timer_t *tm_env_inhome = lv_timer_create(tm_env_inhome_cb,1000,disp);
    lv_timer_set_repeat_count(tm_env_inhome, -1);

    // lv_demo_benchmark();
    while (1) {
        // raise the task priority of LVGL and/or reduce the handler period can improve the performance
        vTaskDelay(pdMS_TO_TICKS(10));
        // The task running lv_timer_handler should have lower priority than that running `lv_tick_inc`
        lv_timer_handler();
    }
}
